#include <IRremote.h>
#include <U8glib.h> 
#include "key.h"
#include "zh-cn.h"
#include "TaskScheduler.h"
#include "DHT.h"

#include <OneWire.h>
#include <DallasTemperature.h>

//红外接收
int IR_RECV_PIN = 7; 
IRrecv irrecv(IR_RECV_PIN);
decode_results results;


// 温湿度检测
#define DHTPIN A6       //温湿度输入
#define DHTTYPE DHT21   // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);

uint8_t redraw_required = 0; //重绘中

uint8_t in_settings = 0; //设置中

uint8_t in_time = 0; //计时中

uint8_t setting_temperature = 0; //设置温度

uint8_t count_down_minute = 0; //分钟倒计时

const uint8_t count_1[] PROGMEM = {0x00,0x60,0x30,0x60,0x18,0x60,0x18,0x60,0x00,0x60,0x00,0x60,0xFF,0xFF,0x18,0x60,
0x18,0x60,0x18,0x60,0x18,0x60,0x18,0x60,0x1E,0x60,0x1C,0x60,0x18,0x60,0x00,0x60};

const uint8_t count_2[] PROGMEM = {0x00,0x0C,0x00,0x0C,0x7E,0x0C,0x66,0x0C,0x67,0xFF,0x66,0x0C,0x66,0x0C,0x7E,0x0C,
0x66,0xCC,0x66,0x6C,0x66,0x6C,0x66,0x0C,0x7E,0x0C,0x66,0x0C,0x00,0x3C,0x00,0x18};

//当前
float current_temperature = 22;
uint8_t current_humidity = 50;

//设定
uint8_t set_temperature = 28;
uint8_t set_humidity = 75;

//继电器
uint8_t hotControl = 3;    //D3引脚控制加热
uint8_t humControl = 5;    //D5引脚控制加湿器

void changeSettingValue(int8_t val){
  if(in_settings){
    if(setting_temperature){
      set_temperature=set_temperature + val;
    }else{
      set_humidity=set_humidity + val*5;
    }
  }
  redraw_required = 1;
}

void onkeydown(unsigned long keyCode){
  switch (keyCode){
    case KEY_SET:
      in_settings = 1;
      setting_temperature = 1 - setting_temperature;
      redraw_required = 1;
      break;
    case KEY_OK:
    case KEY_BACK:
      in_settings = 0;
      setting_temperature = 0;
      redraw_required = 1;
      break;
    case KEY_UP:
      changeSettingValue(1);
      break;  
    case KEY_DOWN:
      changeSettingValue(-1);
      break;
    case KEY_MENU:
      in_time = 1 - in_time;
      break;
    default:
      break;
  }

  // Serial.println(in_settings);
  // Serial.println(setting_temperature);
  Serial.print("Settings: t:");
  Serial.print(set_temperature);
  Serial.print(", h:");
  Serial.println(set_humidity);
}

void draw(void) {
  uint8_t line1 = 27;
  uint8_t line2 = 59;
  uint8_t start = 3;
  u8g.setFont(u8g_font_fur20);
  u8g.setPrintPos(start, line1);
  u8g.print(current_temperature);
  u8g.setPrintPos(start, line2);
  u8g.print(current_humidity);

  start = 36;
  u8g.drawStr(start-1, line1, "`C");
  u8g.drawStr(start, line2, "%");

  line1 = 15;
  line2 = 47;
  start = 68;
  u8g.setFont(u8g_font_7x13);
  u8g.drawStr(start, line1, "->");
  u8g.drawStr(start, line2, "->");
  u8g.setPrintPos(start+16, line1);
  u8g.print(set_temperature);
  u8g.setPrintPos(start+16, line2);
  u8g.print(set_humidity);
  u8g.drawStr(start+16+15, line1, "`C");
  u8g.drawStr(start+16+16, line2, "%");

  line1 = 28;
  line2 = 58;
  if(current_temperature < set_temperature){
    u8g.setDefaultForegroundColor();  
    u8g.drawBox(start-1, line1-10, u8g.getStrWidth("heating")+1, 13);
    u8g.setDefaultBackgroundColor();
    u8g.drawStr(start, line1, "heating");
    u8g.setDefaultForegroundColor();
  }else{
    u8g.drawStr(start+10, line1, "keep");
  }

  if(current_humidity < set_humidity){
    u8g.drawStr(start, line2, "unsuite");
  }else{
    u8g.drawStr(start+6, line2, "suited");
  }
  if(in_settings){
    if(setting_temperature){
      u8g.drawFrame(0, 0, 128, 33);
    }else{
      u8g.drawFrame(0, 32, 128, 32);
    }

  }else{
    u8g.drawLine(0, 32, 128, 32); //分割线
  }
}

void drawTime(void) {
  	
  u8g.drawBitmapP( 10, 5, 2, 16, count_1);
  u8g.drawBitmapP( 26, 5, 2, 16, count_2);

  // u8g.setFont(u8g_font_fur14);
  // u8g.setPrintPos(3, 20);
  // u8g.print("Count down:");
  // u8g.setPrintPos(10, 50);
  // u8g.print(count_down_minute);

  // u8g.drawBitmapP ( 96 , 32 , 2 , 16 , bitmap_z); 
}

void getCurrentTmpHum(){
  uint8_t t = uint8_t(dht.readTemperature()); //读取温度，保留整数
  uint8_t h = uint8_t(dht.readHumidity());    //读取湿度，保留整数
  
  if (isnan(h) || isnan(t) ) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  current_temperature = t;
  current_humidity = h;
  //current_temperature++;
  Serial.print("T:");
  Serial.print(current_temperature);
  Serial.print(" H:");
  Serial.println(current_humidity);
  redraw_required = 1;
}



void compareValue(){
  if(current_temperature < set_temperature - 1){
    digitalWrite(hotControl, HIGH);
  }else{
    digitalWrite(hotControl, LOW);
  }
  if(current_humidity < set_humidity){
    digitalWrite(humControl, HIGH);
  }else{
    digitalWrite(humControl, LOW);
  }
}

void setup()
{
  pinMode(hotControl, OUTPUT);
  pinMode(humControl, OUTPUT);

  dht.begin();
  
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  // Serial.println("enableIRIn Success.");  

  Sch.init();
  Sch.addTask(getCurrentTmpHum, 500, 1000, 1); //从第2s开始读取温度，每隔 2s更新一次显示数据
  Sch.start();  // Start the task scheduler

  redraw_required = 1;
}

void loop() {

  Sch.dispatchTasks();

  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    onkeydown(results.value);

    irrecv.resume(); // Receive the next value
  }
  delay(50);

  if (  redraw_required != 0 ) { //不处于绘制中，启动绘制
    u8g.firstPage();
    do{
      // if(in_time == 1){
      //   drawTime();
      // }else{
      //   draw();
      // }     
      draw(); 
    } while (u8g.nextPage());
    redraw_required = 0;
  }
  delay(50);

  compareValue(); //对比温湿度数据
  delay(50);
}